# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def tree2str(self, t):
        """
        :type t: TreeNode
        :rtype: str
        """
        if not t :
            return ""
        res = str(t.val)
        # if t.left and  t.right:
        #     res += ('('+ self.tree2str(t.left) + ')')
        #     res += ('(' + self.tree2str(t.right) + ')')
        #
        # elif t.left and not t.right:
        #     res += ('(' + self.tree2str(t.left) + ')')
        # elif not t.left and t.right:
        #     res += ('(' + ')')
        #     res += ('(' + self.tree2str(t.right) + ')')
        # return res
        if t.left or t.right:
            res += ('(' + self.tree2str(t.left) + ')')
        if t.right:
            res += ('(' + self.tree2str(t.right) + ')')
